package cn.getech.data.development.config.properties;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.security.GeneralSecurityException;
import java.util.Properties;

@Component
public class SendEMailConfig {


    @Value("${mail.ssl:false}")
    private String mailSslTrue;

    @Bean
    public Properties configs() {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "mail.tcl.com"); // 读取不到配置文件，暂时写死
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.smtp.port", "587");
        // 开启安全协议，SSL加密
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        System.out.println("mailSslTrue="+mailSslTrue);
        if(mailSslTrue!=null&&mailSslTrue.trim().equals("true")){
            prop.put("mail.smtp.ssl.enable", "true");
            prop.put("mail.smtp.ssl.socketFactory", sf);
        }
        return prop;
    }
}
